package com.fourhats.services.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import com.fourhats.dao.CategoryDAO;
import com.fourhats.model.Category;
import com.fourhats.services.CategoryService;
import com.fourhats.services.dto.CategoryDto;

public class CategoryServiceImpl implements CategoryService {

	@Autowired
	private Mapper mapper;
	
	@Autowired
	private CategoryDAO categoryDAO;
	
	public List<CategoryDto> getCategories() {
		return this.getDtos(categoryDAO.getCategories());
	}
	
	public List<CategoryDto> getCategoriesByKeyword(String keyword) {
		List<Category> categories = categoryDAO.getCategoriesByKeyword(keyword);
		return this.getDtos(categories);
	}
	
	public CategoryDto getCategoryById(long categoryId) {
		return mapper.map(categoryDAO.getById(categoryId), CategoryDto.class);
	}
	
	private List<CategoryDto> getDtos(List<Category> categories) {
		List<CategoryDto> dtos = new ArrayList<CategoryDto>();
		for(Category i : categories) {
			dtos.add(mapper.map(i, CategoryDto.class));
		}
		
		return dtos;
	}
}